package com.mmall.concurrency.example.atomic;


import com.mmall.concurrency.annoations.NotThreadSafe;
import lombok.Getter;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;

@NotThreadSafe
public class AtomicExample4 {
    private static AtomicIntegerFieldUpdater<AtomicExample4> updater = AtomicIntegerFieldUpdater.newUpdater(AtomicExample4.class,"count");

    @Getter
    public volatile int count = 100;

    private static AtomicExample4 example4 = new AtomicExample4();

    public static void main(String[] args){
        if(updater.compareAndSet(example4,100,120)){
            System.out.println("update success 1{}"+example4.getCount());
        }
        if(updater.compareAndSet(example4,100,120)){
            System.out.println("update success 2{}"+example4.getCount());
        }else {
            System.out.println("update failed {}"+example4.getCount());
        }
    }

}
